﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeepZoomPivotConstructor.VisualLib;

namespace DeepZoomPivotConstructor.PivotLib
{
    /// <summary>
    /// A single pivot image along with its meta-data.
    /// </summary>
    public class PivotImage : IEnumerable<PivotMetadataBase>
    {
        /// <summary>
        /// New pivot image, with a paricular visual as the main thing
        /// </summary>
        /// <param name="v"></param>
        public PivotImage(Visual v)
        {
            Image = v;
        }

        /// <summary>
        /// Kepe track of all the pivot meta data we need!
        /// </summary>
        private List<PivotMetadataBase> _metaData = new List<PivotMetadataBase>();

        /// <summary>
        /// Add a bit of pivot meta data
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void Add(string name, string value)
        {
            _metaData.Add(new PivotMetadataString(name, value));
        }

        /// <summary>
        /// Add a LiNK meta data to the image
        /// </summary>
        /// <param name="name"></param>
        /// <param name="u"></param>
        public void Add(string name, string linkName, Uri u)
        {
            _metaData.Add(new PivotMetadataURL(name, linkName, u));
        }

        /// <summary>
        /// Add a string meta date to the image
        /// </summary>
        /// <param name="name"></param>
        /// <param name="number"></param>
        public void Add(string name, decimal number)
        {
            _metaData.Add(new PivotMetadataNumber(name, number));
        }

        /// <summary>
        /// Add a Date/Time facet to the image.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="when"></param>
        public void Add(string name, DateTime when)
        {
            _metaData.Add(new PivotMetadataDateTime(name, when));
        }

        /// <summary>
        /// Get the Image Visual that holds the thing we want to look at.
        /// </summary>
        public Visual Image { get; private set; }

        /// <summary>
        /// Get/Set the name of this image. Used to map to the Pivot image
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Return an iterator of all the meta data we own!
        /// </summary>
        /// <returns></returns>
        public IEnumerator<PivotMetadataBase> GetEnumerator()
        {
            return _metaData.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get/Set the description of this image.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Returns the meta data of type T1 and name.
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <param name="p"></param>
        /// <returns></returns>
        public IEnumerable<T1> GetMetadata<T1>(string metadataName)
            where T1 : PivotMetadataBase
        {
            return from m in _metaData
                   where m.Name == metadataName && m.GetType() == typeof(T1)
                   select (T1) m;
        }
    }
}
